
from esrk import *
# 创建索引
create_index('rank_index')
# 电影名称
# from lxml import etree
# import requests
# url = 'https://movie.douban.com/top250'
# header={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"}
# data = requests.get(url,headers=header).text
# s = etree.HTML(data)
# title = s.xpath('//*[@id="content"]/div/div[1]/ol/li/div/div[2]/div[1]/a/span[1]/text()')
# for movename in title:
#     print(movename)

# 评分
# from lxml import etree
# import requests
# url = 'https://movie.douban.com/top250'
# header={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"}
# data = requests.get(url,headers=header).text
# s = etree.HTML(data)
# title = s.xpath('//*[@id="content"]/div/div[1]/ol/li/div/div[2]/div[2]/div/span[2]/text()')
# for score in title:
#     # print(type(score))    #输出类型  是一个节点结果 lxml.etree._ElementUnicodeResult
#     print(score)

#电影和评分
# from lxml import etree
# import requests
# url = 'https://movie.douban.com/top250'
# header={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"}
# data = requests.get(url,headers=header).text
# s = etree.HTML(data)
# # 先爬取标签的共同部分    此时不加解析 它就是一个元素 <Element li at 0x31cdb48>
# title = s.xpath('//*[@id="content"]/div/div[1]/ol/li')
# for move_total in title:
#     # print(type(score))    #输出类型  是一个节点结果 lxml.etree._ElementUnicodeResult
#     # print(move_name_score)    #<Element li at 0x31cdb48>
#     move_name = move_total.xpath('./div/div[2]/div[1]/a/span[1]/text()')[0]
#     move_score = move_total.xpath('./div/div[2]/div[2]/div/span[2]/text()')[0]
#     # print(move_name)
#     # print(move_score)
#     print("{} {}".format(move_name,move_score))

# 分开存储电影名称和评分
from lxml import etree
import requests
from bs4 import BeautifulSoup
import os

for i in range(10):
    url = 'https://movie.douban.com/top250?start={}'.format(i*25)
    # print(url) 每页25条
    header={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"}
    data = requests.get(url,headers=header,timeout=100).text
    s = etree.HTML(data)
    # 先爬取标签的共同部分    此时不加解析 它就是一个元素 <Element li at 0x31cdb48>
    file = s.xpath('//*[@id="content"]/div/div[1]/ol/li')
    # # 存放名字
    # move_name_list = []
    # # 存放评分
    # move_score_list = []
    # # 图片
    # # imgObj = []
    # # 存放名字对应链接
    # move_link_list = []
    for move_total in file:
        # print(type(score))    #输出类型  是一个节点结果 lxml.etree._ElementUnicodeResult
        # print(move_name_score)    #<Element li at 0x31cdb48>
        move_link = move_total.xpath('.//a/@href')[0]
        move_name = move_total.xpath('./div/div[2]/div[1]/a/span[1]/text()')[0]
        move_score = move_total.xpath('./div/div[2]/div[2]/div/span[2]/text()')[0]
        #         -------------------------调用insert_index完成插入数据-------------
        insert_index('rank_index', move_link, {
            'title': move_name,
            'Link': move_link,
            'move_score': move_score,
        })
        # print(move_link)
        # with open('./result.txt', 'a', encoding='utf-8')as f:
        #     f.write('title:------->>' + str(move_name) + '        ')
        #     f.write('Link:------->>' + str(move_link) + '        ')
        #     f.write('Content:------->>' + str(move_score) + '\n')
search1('rank_index', '电影')
        # move_img = move_total.xpath('./div/div[1]/a/img/@src')[0]
        # print(move_name)
        # print(move_score)
        # print("{} {}".format(move_name,move_score))
        # move_name_list.append(move_name)
        # move_score_list.append(move_score)
        # move_link_list.append(move_link)
        #爬取图片
        # imgObj.append(move_img)
        # for img in imgObj:
        #     with open(r'C:\Users\Administrator\Desktop\img\\'+os.path.basename(img),'wb') as f:
        #         f.write(requests.get(img).content)
        #         print(os.path.basename(img)+"保存成功")
    # print(len(move_name_list))
    # print(len(move_score_list))
    # print(move_name_list,move_score_list,move_link_list)
    # print(imgObj)





